package com.itheima.ydd.filter;

import com.itheima.ydd.utils.AppJwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter
@Slf4j
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //类型转换
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        log.info("此次访问路径为:{}",request.getRequestURL());
        String token = request.getHeader("x-token");
        //判断token是否有数据
//        if (token!=null){
//            //判断token是否过期
//            Claims claims = AppJwtUtil.tokenToOut(token);
//            int i = AppJwtUtil.verifyToken(claims);
//            if (i==-1 || i==0){
//                filterChain.doFilter(request,response);
//            }
//        }
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
